---@class Battle.SkillSystem:Battle.EntityComponent
local SkillSystem = Battle.CreateClass("SkillSystem",Battle.EntityComponent)
Battle.SkillSystem = SkillSystem
function SkillSystem:OnCreate()
    ---@type Battle.Skill[]
    self.skillList = {}     --所有技能
    ---@type Battle.Skill[]
    self.releaseList = {} --释放中的技能
    self:RegUpdate(true)
    self.normalSkillId = 0
    self.skillId1 = 0

end

function SkillSystem:OnInitialized()
    for k,v in pairs(self.skillList) do
        v:BaseInitialized()
    end
end

function SkillSystem:OnDestroy()
    self.actor = nil
    if self.skillList then
        for k, v in pairs(self.skillList) do
            v:OnDestroy()
        end
    end
    self.skillList = nil
end

function SkillSystem:EndSkill(skillId,force)
    local s = self.releaseList[skillId]
    self.releaseList[skillId] = nil
    if not s then
        return
    end
    s:EndSkill()
    if not force and skillId == self.normalSkillId then
        --使用攻击后
        Battle.EventManager.notifyEventListener(Battle.BuffTriggerType.ATTACK_BACK, self.actor.id)
    end
end
function SkillSystem:EndAllSkill()
    for k, v in pairs(self.releaseList) do
        v:EndSkill()
    end
    table.clear(self.releaseList)
end
function SkillSystem:CreateSkill(skillId)
    local skill = Battle.Skill.New()
    skill:OnCreate(self.actor, skillId)
    if self._isInitialized then
        skill:BaseInitialized()
    end
    self.skillList[skillId] = skill
    return skill
end
---装配技能
function SkillSystem:EquipSkill(skillId)
    if skillId == nil or skillId == 0 then return end
    local skill = self:CreateSkill(skillId)
    if skill.skillData.skillType == Battle.SkillType.Normal  then
        self.normalSkillId = skillId
    elseif skill.skillData.skillType == Battle.SkillType.Active then
        self.skillId1 = skillId
    elseif skill.skillData.skillType == Battle.SkillType.Passive then
        --将被动的buff挂上
        for k,v in pairs(skill.skillData.f_SelfBuff) do
            self.actor:AddBuff(v.buffId,v.layer,nil,self.actor,skillId)
        end
    end
end
---不装配技能
function SkillSystem:UnEquipSkill(skillId)
    local s = self.skillList[skillId]
    if s then
        s:OnDestroy()
        self.releaseList[skillId] = nil
        self.skillList[skillId] = nil
    end
end
---@return Battle.Skill
function SkillSystem:GetSkill(skillId)
    return self.skillList[skillId]
end
function SkillSystem:DoSkill(skillId)
    local s = self.skillList[skillId]
    if not s then
        self:EquipSkill(skillId)
    end
    s = self.skillList[skillId]
    if not self:CheckUseSkill(skillId) then
        return nil;
    end
    ---Interrupt
    for k,v in pairs(self.releaseList) do
        if v.skillData.priority > s.skillData.priority and v.skillData.f_SeparateSkill == 0 then --有高优先级的
            return nil
        end
    end
    for k,v in pairs(self.releaseList) do
        if v.skillData.priority < s.skillData.priority then
            s:End(true)
        end
    end
    if skillId == self.normalSkillId then
        --使用攻击前
        Battle.EventManager.notifyEventListener(Battle.BuffTriggerType.ATTACK_FORT, self.actor.id)
    end
    s:DoSkill()
    self.releaseList[skillId] = s
    return s
end
function SkillSystem:Update(dt)
    for k, v in pairs(self.skillList) do
        v:Update(dt)
    end
end
function SkillSystem:CheckUseSkill(skillId)
    local skill = self:GetSkill(skillId)
    if skill == nil then
        return false
    end
    return skill:CheckUseSkill()
end
function SkillSystem:GetNormalSkillId()
    return self.normalSkillId
end
---@return Battle.Skill
function SkillSystem:GetNormalSkill()
    return self:GetSkill(self.normalSkillId)
end
function SkillSystem:GetSkill1Id()
    return self.skillId1
end
---@return Battle.Skill
function SkillSystem:GetSkill1()
    return self:GetSkill(self.skillId1)
end